<template>
  <div>
    <div class="btn-group-wrapper bottom">
      <el-form :inline="true">
        <el-button-group>
          <el-button type="primary" icon="plus" @click="addHandler"></el-button>
          <el-button type="primary" icon="edit" @click="editHandler"></el-button>
          <el-button type="primary" icon="delete" @click="delHandler"></el-button>
          <el-button type="primary" @click="addcombinationHandler">组合添加基金</el-button>
          <el-button type="primary" @click="addcombinationHistory">组合添加历史数据</el-button>
          <!--<el-button type="primary" @click="addwarehouseHandler">设置调仓记录</el-button>-->
        </el-button-group>
      </el-form>
    </div>


    <el-table ref="bulletinTable" :data="getSysPortfolios" height="500" border :stripe="true"
              :highlight-current-row="true"
              @current-change="tableRowChangeClickHandler" current-row-key="id" style="width: 100%">
      <el-table-column prop="combinationCode" label="组合代码" width="100"/>
      <el-table-column prop="combinationName" label="组合名称" width="180"/>
      <el-table-column prop="limitMoneyPurchased" label="起购" width="100"/>
      <el-table-column prop="runDay" label="已运行天数" width="110"/>
      <el-table-column prop="briefIntroduction" label="简介" :show-overflow-tooltip="true"/>
      <el-table-column prop="baseAnnualizedIncome" label="基准年化收益" width="110"/>
      <el-table-column prop="accumulatedIncome" label="累计收益率" width="110"/>
      <el-table-column prop="annualizedIncome" label="年化收益率" width="110"/>
      <el-table-column prop="dailyCent" label="日涨幅" width="110"/>
      <el-table-column prop="createDate" label="成立日期" width="200"/>
      <el-table-column prop="baseCalculate" label="设置资产值" width="130"/>
      <el-table-column prop="state" label="使用状态" width="110"/>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="getSysPortfoliosCurrentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="getSysPortfoliosTotalCount">
      </el-pagination>
    </div>
    <el-dialog title="文章预览" v-model="showContentDialog">
      <div>
        <p>{{title}}</p>
      </div>
      <div>
        <p v-html="content"></p>
      </div>
    </el-dialog>
  </div>
</template>

<script type="text/babel">
  import { mapActions, mapGetters } from 'vuex'

  import fundPortfolioStore from './bll/fundPortfolioStore'
  import store from '../../store'

  (!store.state.sysfundPortfolio) && store.registerModule('sysfundPortfolio', fundPortfolioStore)

  export default {
    data () {
      return {
        State: 2,
        pageSize: 10,
        formMode: null,
        currentRecord: null,
        queryModel: 0,
        title: '',
        content: '',
        showContentDialog: false
      }
    },
    computed: {
      ...mapGetters(['getSysPortfolios', 'getSysPortfoliosCurrentPage', 'getSysPortfoliosTotalCount'])
    },

    mounted () {
      this.initPage()
    },
    mode (val) {
      this.pageModel = val
    },
    methods: {
      ...mapActions(['querySysPortfolios', 'querySysPortfoliosCount', 'deleteSysPortfolio']),
      initPage () {
        console.log('[Bulletin Page] mounted')
        this.querySysPortfolios()

        this.querySysPortfoliosCount()
        //this.queryGroup('bulletin');
      },
      //addHandler() {
      //  this.currentRecord = null;
      //
      //  // hack table again
      //  let selectedRow = this.$refs.bulletinTable.$el.querySelector('.current-row');
      //  if (selectedRow && selectedRow.classList) {
      //    selectedRow.classList.remove('current-row');
      //  }
      //  this.formMode = 'ADD';
      //  this.$refs.bulletinForm.enable();
      //  this.$refs.bulletinForm.clear();
      //},
      addHandler () {
        this.$router.push('/fundPortfolio/add')
      },
      editHandler () {
        if (!this.currentRecord) {
          this.$message.warning('请选择需要修改的内容')
          return
        }
        console.log(this.currentRecord)
        this.$router.push({
          path: '/fundPortfolio/add',
          query: {combinationCode: this.currentRecord.combinationCode}
        })

      },

      delHandler () {
        if (!this.currentRecord) {
          this.$message.warning('请选择需要删除的内容')
          return
        }
        this.$confirm('确定删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var combinationCode = this.currentRecord.combinationCode
          console.log(combinationCode)
          this.deleteSysPortfolio({combinationCode}).then(() => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            // this.queryBanks()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      addcombinationHandler () {
        if (!this.currentRecord) {
          this.$message.warning('请选择需要修改的内容')
          return
        }
        console.log(this.currentRecord)
        this.$router.push({
          path: '/fundPortfolio/add',
          query: {combinationCode: this.currentRecord.combinationCode, type: 'combination'}
        })
      },
      addcombinationHistory () {
        if (!this.currentRecord) {
          this.$message.warning('请选择需要添加数据的基金')
          return
        }
        console.log(this.currentRecord)
        this.$router.push({
//          path: '/fundPortfolioHistory/index',
          path: '/fundPortfolio/addhistory',
          query: {combinationCode: this.currentRecord.combinationCode, type: 'combination'}
        })
      },
      tableRowChangeClickHandler (currentRow, oldCurrentRow) {
        console.log(currentRow)
        this.currentRecord = currentRow
      },

      enableHandler () {
        console.log('enable')
        this.$message.warning('this function is building...')
      },

      handleSizeChange (val) {
        console.log(`每页 ${val} 条`)
        this.pageSize = val
        this.querySysPortfolios({pageIndex: 1, pageSize: val})

        //this.querySysPortfolios({pageIndex: 1, pageSize: val});
      },
      handleCurrentChange (val) {
        console.log(arguments)
        console.log(`current${val}`)
        this.querySysPortfolios({pageIndex: val, pageSize: this.pageSize})
        //  this.querySysPortfolios({pageIndex: val, pageSize: this.pageSize});
      }
//      addwarehouseHandler () {
//        if (!this.currentRecord) {
//          this.$message.warning('请选择需要修改的内容')
//          return
//        }
//        console.log(this.currentRecord)
//        this.$router.push({
//          path: '/wareHouse/add',
//          query: {warehouseId: this.currentRecord.warehouseId}
//        })
//      }
    }
  }
</script>
